package com.zgs.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.zgs.mall.order.entity.OrderEntity;
import com.zgs.mall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 订单超时--关闭订单
 */
@RabbitListener(queues ="order.release.queue")
@Service
public class OrderCloseListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public  void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        try {
            //关闭订单
            orderService.orderClose(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
